<script setup lang="tsx">
import { onMounted, ref } from 'vue';
import { Button, Popconfirm, Tag } from 'ant-design-vue';
import { CheckCircleOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { useTable, useTableOperate, useTableScroll } from '@/hooks/common/table';
import { $t } from '@/locales';
import { getHostListApi, hostDeleteApi } from '@/service/api/businessApi';
import { useProjectStore } from '@/store/modules/project';
import { getewayHooks } from '@/views/breedingManage/getewayManage/getewayHooks';
import UserOperateDrawer from './modules/operate-drawer.vue';
import UserSearch from './modules/search.vue';

const { tableWrapperRef, scrollConfig } = useTableScroll();
const projectStore = useProjectStore();

// 获取网关数据
const { getGetewayList } = getewayHooks();
const getewayData = ref([]);

onMounted(async () => {
  getewayData.value = await getGetewayList(projectStore.currentProject?.id);
});

const {
  columns,
  columnChecks,
  data,
  getData,
  getDataByPage,
  loading,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: getHostListApi,
  apiParams: {
    pageNo: 1,
    pageSize: 10,
    projectId: projectStore.currentProject?.id,
    online: ''
  },
  columns: (): any => [
    {
      key: 'hostNo',
      dataIndex: 'hostNo',
      title: '终端编码',
      width: 160,
      fixed: 'left'
    },
    {
      key: 'hostName',
      dataIndex: 'hostName',
      title: '终端名称',
      width: 160
    },
    {
      key: 'collectionName',
      dataIndex: 'collectionName',
      title: '网关名称',
      width: 160
    },
    {
      key: 'collectionNo',
      dataIndex: 'collectionNo',
      title: '网关编码',
      width: 160
    },
    {
      key: 'firmwareVersion',
      dataIndex: 'firmwareVersion',
      title: '固件版本',
      width: 160
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 160
    },
    {
      key: 'online',
      dataIndex: 'online',
      title: '在线状态',
      width: 160,
      customRender: ({ record }: any) => {
        if (record.online === true) {
          return (
            <Tag color="success" icon={<CheckCircleOutlined />}>
              在线
            </Tag>
          );
        }
        return (
          <Tag color="warning" icon={<ExclamationCircleOutlined />}>
            离线
          </Tag>
        );
      }
    },
    {
      title: '创建时间',
      dataIndex: 'gmtCreated',
      key: 'gmtCreated',
      width: 160
    },
    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      width: 140,
      fixed: 'right',
      customRender: ({ record }: any) => (
        <div class="flex-center gap-8px">
          <Button type="primary" ghost size="small" onClick={() => edit(record.id)}>
            编辑
          </Button>
          <Popconfirm title="确认删除吗？" onConfirm={() => handleDelete(record.id)}>
            <Button danger size="small">
              删除
            </Button>
          </Popconfirm>
        </div>
      )
    }
  ]
});

const { drawerVisible, operateType, editingData, handleAdd, handleEdit, checkedRowKeys, onDeleted } = useTableOperate(
  data as any,
  getData
);

async function handleDelete(id: any) {
  const res = await hostDeleteApi(id);
  if (res.error === null) await onDeleted();
}

function edit(id: number) {
  handleEdit(id);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <UserSearch
      v-model:model="searchParams"
      v-model:getewayData="getewayData"
      @reset="resetSearchParams"
      @search="getDataByPage"
    />
    <ACard
      title="终端列表"
      :bordered="false"
      :body-style="{ flex: 1, overflow: 'hidden' }"
      class="flex-col-stretch sm:flex-1-hidden card-wrapper"
    >
      <template #extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          @add="handleAdd"
          @refresh="getData"
        />
      </template>
      <ATable
        ref="tableWrapperRef"
        :columns="columns"
        :data-source="data"
        size="small"
        :scroll="scrollConfig"
        :loading="loading"
        row-key="id"
        :pagination="mobilePagination"
        class="h-full"
        children-column-name="childDeviceInfo"
      />

      <UserOperateDrawer
        v-model:visible="drawerVisible"
        :operate-type="operateType"
        :row-data="editingData"
        @submitted="getData"
      />
    </ACard>
  </div>
</template>

<style scoped></style>
